package com.bytedance.article.common.c;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;

import com.bytedance.common.b.f;
import com.bytedance.common.utility.Logger;
import com.bytedance.frameworks.baselib.network.http.exception.HttpResponseException;
import com.bytedance.ttnet.a.AppConfig;
import com.ss.android.common.AppConsts;
import com.ss.android.common.app.permission.PermissionsManager;
import com.ss.android.common.util.LoadUrlUtils;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.newmedia.webview.SSWebView;

import org.apache.http.conn.ConnectTimeoutException;

import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLPeerUnverifiedException;

public final class TTUtils {
    private static ColorFilter sNightModeColorFilter = new ColorMatrixColorFilter(new float[]{0.5f, 0f, 0f, 0f, 0f, 0f, 0.5f, 0f, 0f, 0f, 0f, 0f, 0.5f, 0f, 0f, 0f, 0f, 0f, 1f, 0f});
    private static boolean isScreenSizeCached = false;
    private static int screenWidth;
    private static int screenHeight;

    public static int a(int arg1) {
        int v0;
        switch (arg1) {
            case 12: {
                v0 = com.ss.android.article.browser.R.string.ss_error_no_connections;
                break;
            }
            case 13: {
                v0 = com.ss.android.article.browser.R.string.ss_error_connect_timeout;
                break;
            }
            case 14: {
                v0 = com.ss.android.article.browser.R.string.ss_error_network_timeout;
                break;
            }
            case 15: {
                v0 = com.ss.android.article.browser.R.string.ss_error_network_error;
                break;
            }
            case 16: {
                v0 = com.ss.android.article.browser.R.string.ss_error_server_error;
                break;
            }
            case 17: {
                v0 = com.ss.android.article.browser.R.string.ss_error_api_error;
                break;
            }
            case 19: {
                v0 = com.ss.android.article.browser.R.string.ss_error_service_unavailable;
                break;
            }
            default: {
                v0 = com.ss.android.article.browser.R.string.ss_error_unknown;
                break;
            }
        }

        return v0;
    }

    public static int getScreenWidth(Context context) {
        if (!isScreenSizeCached) {
            cacheScreenSize(context);
        }

        return screenWidth;
    }

    public static int getErrorCode(Context context, Throwable throwable) {
        int errorCode;
        if ((throwable instanceof ConnectTimeoutException)) {
            errorCode = 13;
        } else if ((throwable instanceof SocketTimeoutException)) {
            errorCode = 14;
        } else if ((throwable instanceof SocketException)) {
            Logger.v("TTUtils", "api socket exception: " + throwable);
            errorCode = 15;
        } else if ((throwable instanceof SSLPeerUnverifiedException)) {
            errorCode = 21;
            Logger.v("TTUtils", "api ssl exception: " + throwable);
        } else if ((throwable instanceof HttpResponseException)) {
            errorCode = ((HttpResponseException) throwable).getStatusCode() == 503 ? 19 : 16;
            Logger.w("TTUtils", "server error: " + throwable);
        } else {
            if ((throwable instanceof IOException)) {
                Logger.v("TTUtils", "api io exception: " + throwable);
                errorCode = 15;
            } else {
                errorCode = 18;
                Logger.w("TTUtils", "api exception: " + throwable);
            }
        }

        if (context != null && (errorCode == 15 || errorCode == 14) && !NetworkUtils.isNetworkAvailable(context)) {
            errorCode = 12;
        }

        return errorCode;
    }

    public static ColorFilter getNightModeColorFilter() {
        return sNightModeColorFilter;
    }

    public static String a(Context arg8, Uri arg9) {
        Cursor v7_1;
        Cursor v1;
        String v6 = null;
        if (arg9 != null) {
            String v0 = arg9.getScheme();
            if (!android.support.design.a.isTextEmpty(v0) && !"file".equals(v0)) {
                if ("http".equals(v0)) {
                    v6 = arg9.toString();
                } else if ("content".equals(v0)) {
                    String[] v2 = new String[]{"_data"};
                    String v7 = "";
                    v1 = arg8.getContentResolver().query(arg9, v2, null, null, null);
                    if (v1.moveToFirst()) {
                        v0 = v1.getString(0);
                    } else {
                        v0 = v7;
                    }
                    if (v1 != null) {
                        try {
                            v1.close();
                        } catch (Throwable v0_1) {

                        }
                    }

                    v6 = v0;
                    v7_1 = v1;
                    label_41:
                    if (!android.support.design.a.isTextEmpty(v6)) {
                        return v6;
                    }

                    ContentResolver v0_3 = arg8.getContentResolver();
                    String v3_1 = "_id= ?";
                    String v1_2 = arg9.getLastPathSegment();
                    if (Build.VERSION.SDK_INT >= 19 && !android.support.design.a.isTextEmpty(v1_2) && (v1_2.contains(":"))) {
                        v1_2 = v1_2.split(":")[1];
                    }

                    v7_1 = v0_3.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, v2, v3_1, new String[]{v1_2}, null);
                    if (v7_1.moveToFirst()) {
                        v6 = v7_1.getString(0);
                    }

                    if (v7_1 != null) {
                        v7_1.close();
                    }

                    if (v7_1 == null) {
                        return v6;
                    }

                    try {
                        v7_1.close();
                    } catch (Exception v0_2) {
                    }
                } else {

                }

                return v6;
            }

            v6 = arg9.getPath();
        }

        return v6;
    }

    public static void a(Activity arg4, Fragment arg5, int arg6) {
        PermissionsManager.getInstance().requestPermission(arg4, new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, new c(arg4, arg5, arg6));
    }

    public static void a(Activity arg8, Fragment arg9, int arg10, String arg11, String arg12) {
        PermissionsManager.getInstance().requestPermission(arg8, new String[]{"android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"}, new com.bytedance.article.common.c.d(arg8, arg11, arg12, arg9, arg10));
    }

    public static void a(View arg0, Drawable arg1) {
        f.a(arg0, arg1);
    }

    public static void a(String arg1, WebView arg2) {
        a(arg1, arg2, (String) null);
    }

    public static void a(String arg3, WebView arg4, String arg5) {
        if (arg4 != null && !android.support.design.a.isTextEmpty(arg3)) {
            boolean v1 = isValidUrl(arg3);
            String referer = !v1 || !android.support.design.a.isTextEmpty(arg5) ? arg5 : AppConsts.http_refer;
            if (!v1) {
                referer = null;
            }

            HashMap v1_1 = new HashMap();
            if (!android.support.design.a.isTextEmpty(referer)) {
                v1_1.put("Referer", referer);
            }

            webViewLoadUrl(arg3, arg4, v1_1);
        }
    }

    public static boolean isValidUrl(String arg2) {
        boolean v0 = false;
        if (!android.support.design.a.isTextEmpty(arg2) && ((arg2.startsWith("http://")) || (arg2.startsWith("https://")))) {
            v0 = true;
        }

        return v0;
    }

    public static void webViewLoadUrl(String url, WebView webView, HashMap additionalHttpHeaders) {
        if (webView != null && !android.support.design.a.isTextEmpty(url)) {
            boolean isValidUrl = isValidUrl(url);
            Context context = webView.getContext();
            if (!(webView instanceof SSWebView) && (isValidUrl) && context != null) {
                url = AppConfig.getInstance(context).c(url);
            }

            if (!additionalHttpHeaders.isEmpty()) {
                webView.loadUrl(url, ((Map) additionalHttpHeaders));
                return;
            }

            LoadUrlUtils.loadUrl(webView, url);
        }
    }

    public static boolean a(Context arg3, int arg4, int arg5) {
        Resources v0 = arg3.getResources();
        int v1 = v0.getDimensionPixelSize(com.ss.android.article.browser.R.dimen.fj);
        int v0_1 = v0.getDimensionPixelSize(com.ss.android.article.browser.R.dimen.fi);
        boolean v0_2 = arg4 < v1 || arg5 < v0_1 ? false : true;
        return v0_2;
    }

    public static int getScreenHeight(Context arg1) {
        if (!isScreenSizeCached) {
            cacheScreenSize(arg1);
        }

        return screenHeight;
    }

    public static boolean c(Context arg10) {
        int v3;
        int v5;
        TypedArray v0_2;
        TypedArray v2;
        int v4;
        int v6 = -16777216;
        boolean v0 = true;
        if (Build.VERSION.SDK_INT > 20 && arg10 != null) {
            v4 = arg10.getResources().getColor(com.ss.android.article.browser.R.color.o0);
            v2 = arg10.obtainStyledAttributes(com.ss.android.article.browser.R.style.NotificationTitle, new int[]{16842904, 16842901});
            v5 = v2.getColor(0, 0);
            if (v4 == v5) {
                v3 = 1;
            } else {
                v3 = v4 | v6;
                v6 |= v5;
                int v7 = Color.red(v3) - Color.red(v6);
                int v8 = Color.green(v3) - Color.green(v6);
                v3 = Color.blue(v3) - Color.blue(v6);
                double v6_1 = Math.sqrt(((double) (v3 * v3 + (v7 * v7 + v8 * v8))));
                Logger.d("TTUtils", "value:" + v6_1);
                if (v6_1 < 120) {
                    v3 = 1;
                } else {
                    v3 = 0;
                }
            }
            if (v3 == 0) {
                return false;
            }
            Logger.d("TTUtils", "similar color:" + v4 + " " + v5);
            if (v2 == null) {
                return v0;
            }
            try {
                v2.recycle();
            } catch (Throwable v1) {
            }
            return v0;
        } else {
            v0 = false;
        }
        return v0;
    }

    private static void cacheScreenSize(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        Object v0 = context.getSystemService(Context.WINDOW_SERVICE);
        ((WindowManager) v0).getDefaultDisplay().getMetrics(metrics);
        int orientation = ((WindowManager) v0).getDefaultDisplay().getOrientation();
        if (orientation == Surface.ROTATION_90 || orientation == Surface.ROTATION_270) {
            screenWidth = metrics.heightPixels;
            screenHeight = metrics.widthPixels;
        } else {
            screenWidth = metrics.widthPixels;
            screenHeight = metrics.heightPixels;
        }

        isScreenSizeCached = true;
    }
}

